package com.shop.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class OutputImage extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * Destruction of the servlet. <br>
	 */
	private Session session;

	public void destroy() {
		try {
			session.close();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occure
	 */
	public void init() throws ServletException {
		try {
			Configuration config = new Configuration().configure();
			SessionFactory sf = config.buildSessionFactory();
			session = sf.openSession();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
//		try {
//			User user = (User) session.load(User.class, new Integer(1));
//			Blob photo = user.getPhoto();
//			InputStream in = photo.getBinaryStream();
//
//			response.setContentType("image/gif");
//			
//			OutputStream out = response.getOutputStream();
//			byte[] buf = new byte[1024];
//			int len;
//			while ((len = in.read(buf)) != -1) {
//				out.write(buf, 0, len);
//			}
//			in.close();
//			out.close();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
	}

}
